<?php
include('../config.php');
include(PUNG_ROOT . '/admin/header.php');

if($_GET['do'] == 'logout')
{
	session_destroy();
	unset($_SESSION[SESSION_ADMIN]);
	header('Location: ' . PUNG_URL);
}

if(isset($_POST['submit']))
{
	
	$username = mysql_real_escape_string($_POST['username']);
	$password = mysql_real_escape_string($_POST['password']);
	
	if($username == ADMIN_USER && $password == ADMIN_PASS)
	{
		$_SESSION[SESSION_ADMIN] = ADMIN_USER;
		header('Location: index.php');
	}
	else
	{
		redirect_with_message('error', 'LANG_109');
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
	<title><?php print LANG_010 . ' - ' . LANG_106; ?></title>
	<meta content="text/html; charset=utf-8" http-equiv="content-type" />
	<link href="<?php print PUNG_URL . '/admin/'; ?>admin_style.css" rel="stylesheet" type="text/css" />    
    <script type="text/javascript" src="<?php print PUNG_URL;?>/scripts/jquery-1.4.2.js"></script>
    <script type="text/javascript" src="<?php print PUNG_URL;?>/scripts/jquery.validate.js"></script>
	<script type="text/javascript" src="<?php print PUNG_URL;?>/scripts/jvibrate/vibrate.js"></script>
</head>

<body>
<div class='topbar'>
  <table cellpadding='0' cellspacing='0' width='100%'>
  <tr>
  <td valign='top'><img src='<?php print PUNG_URL;?>/admin/images/admin_icon.gif' border='0'></td>
  <td valign='top' align='right'><img src='<?php print PUNG_URL;?>/admin/images/admin_watermark.gif' border='0'></td>
  </tr>
  </table>
</div>
<div id="content">
<div class="box" style="width: 325px; min-height: 300px; margin-top: 40px; margin-left: auto; margin-right: auto;">
  <div class="left"></div>
  <div class="right"></div>
  <div class="heading">
    <h1 style="background-image: url('<?php print PUNG_URL;?>/admin/images/lockscreen.png');"><?php print LANG_106; ?></h1>
  </div>
  <div class="content" style="min-height: 150px;">
        <form action="" method="post" enctype="multipart/form-data" id="form">
      <table style="width: 100%;">
        <tbody><tr>
          <td style="text-align: center;" rowspan="4"><img class="bigKey" src="<?php print PUNG_URL;?>/admin/images/login.png" alt="<?php print LANG_106; ?>"></td>
        </tr>
        <tr>
          <td><?php print LANG_107; ?> :<br>
            <input type="text" name="username" id="username" style="margin-top: 4px;">
            <br>
            <br>
            <?php print LANG_108; ?>:<br>
            <input type="password" name="password" id="password"  style="margin-top: 4px;"></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td style="text-align: right;">
          	<span class="loading"></span><a class="button"><span><?php print LANG_106; ?></span></a>
         	<input type="submit" name="submit" value="Submit" />
          </td>
        </tr>
      </tbody></table>
          </form>
          <?php
			if(isset($_GET['error']))
			{
				print '<p class="warning">' . LANG_109 . '</p>';
			}
			?>
  </div>
</div>
</div>

</body>

</html>

<script type="text/javascript">

$(function() {
	
	function doLogin(){
		var user = $('#username').val();
		var pass = $('#password').val();
        /* do your AJAX call and processing here...*/
        $.ajax({
			type :'POST',
			url	 :'admin_query.php',
			data :{doLogin:true, uname:user, pwd:pass},
			dataType:'json',
			beforeSend:function(){$('.loading').html('<img src="<?php print PUNG_URL; ?>/images/loading.gif">');},
			success:function(msg){
				//alert(msg);
				$('.loading').html('');
				if(msg.status == 0){
					
					// configurations for the buzzing effect. Be careful not to make it too annoying!
					var conf = {
							frequency: 5000,
							spread: 15,
							duration: 1000
						};
					
					
					// this is the call we make when the AJAX callback function indicates a login failure 
					$(".bigKey").vibrate(conf);
					
					// let's also display a notification
					if($(".warning").text() == ""){
						$("#form").append('<p class="warning"><?php print LANG_109;?></p>');

						// clear the fields to discourage brute forcing 						
						$("#username").val("");
						$("#password").val("");	
						$("#username").focus();

						return false;
					}
				}else if(msg.status == 1){
					location.href='?m=index';
				}
				
			} 
		});        
        	
	}
	
	
    $(".button").click( function() {
	//	doLogin();
    });
	
	$('#form input').keydown(function(e) {
		if (e.keyCode == 13) {
			doLogin();
			return false;
		}
	});
    
});
</script>